<?php
class brand extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->load->model("brand_model");
        $this->load->library('pagination');
        $this->load->helper('url');
    }
    public function index()
    {
        $data['title'] = "List Brand";
        $data['path'] = 'brand/list' ;

        //css pagination
        $config['full_tag_open'] = '<ul class="tsc_pagination tsc_paginationA tsc_paginationA01">';
        $config['full_tag_close'] = '</ul>';
        $config['prev_link'] = '&lt;';
        $config['prev_tag_open'] = '<li>';
        $config['prev_tag_close'] = '</li>';
        $config['next_link'] = '&gt;';
        $config['next_tag_open'] = '<li>';
        $config['next_tag_close'] = '</li>';
        $config['cur_tag_open'] = '<li class="current"><a href="#">';
        $config['cur_tag_close'] = '</a></li>';
        $config['num_tag_open'] = '<li>';
        $config['num_tag_close'] = '</li>';

        $config['first_tag_open'] = '<li>';
        $config['first_tag_close'] = '</li>';
        $config['last_tag_open'] = '<li>';
        $config['last_tag_close'] = '</li>';

        $config['first_link'] = '&lt;&lt;';
        $config['last_link'] = '&gt;&gt;';
        //end

        $config['uri_segment'] = 4;
        $config['per_page'] = 4;
        $config['total_rows'] = $this->brand_model->totalBrand();
        $config['base_url'] = base_url().'admin/brand/index';
        $this->pagination->initialize($config);
        $start = $this->uri->segment(4);
        $data['listBrand'] = $this->brand_model->listBrand($config['per_page'],$start);
        $this->load->view("index",$data);
    }
    public function SortName() {
        $data['title'] = "List Brand";
        $data['path'] = 'brand/list' ;
        $results = $this->brand_model->get_item_list();
        $data['listBrand'] = $results['item_list'];
        $this->load->view('index', $data);
    }

    public function insertBrand(){
        $data['title'] = "Add Brand";
        $data['path'] = "brand/add";
        $data['errors'] = "";
        if ($this->input->post('submit') != NULL) {
            $this->form_validation->set_rules('name', 'CategoryName', 'required');
            if ($this->form_validation->run() == NULL) {
                $this->load->view("index", $data);
            } else {
                $name = $this->input->post('name');
                $desciption = $this->input->post('desciption');
                $parent = $this->input->post('parent');
                $dataarray = array(
                    'cate_name' => $name,
                    'cate_description' => $desciption,
                    'cate_parent' => $parent
                );
                $checkname = $this->category_model->checkCate_add($name);
                if ($checkname == FALSE) {
                    $data['errors'] = "Danh mục đã tồn tại";
                    $this->load->view("index", $data);
                } else {
                    $this->category_model->addCate($dataarray);
                    redirect(base_url() . "admin/category");
                    exit();
                }
            }
        } else {
            $this->load->view("index", $data);
        }
    }

    public function deleteBrand(){
        $id = $this->uri->segment(4);
        $this->brand_model->delBrand($id);
        redirect(base_url()."index.php/admin/brand");
        exit();
    }
    public function deleteProduct(){
        $id = $this->uri->segment(4);
        $this->brand_model->delBrand($id);
        $this->brand_model->delProduct($id);
        redirect(base_url()."index.php/admin/brand");
        exit();
    }
}